<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>构建图</title>
</head>
<body>
<script>
    function Vertex(label){
        this.label=label;
    }
    function Graph(v){
        this.v=v;
        this.edges=0;
        this.adj=[];
        for(var i=0;i<this.v;i++){
            this.adj[i]=[];
            this.adj[i].push('');
        }
    }
    Graph.prototype={
        addEdge: function (v,w) {
            this.adj[v].push(w);
            this.adj[w].push(v);
            this.edges++;
        },
        toString: function () {

        },
        show: function () {
            var s='';
            for(var i=0;i<this.v;i++){
                s+='\n顶点-'+i+':';
                for(var j=0;j<this.v;j++){
                    if(undefined!==this.adj[i][j]){
                        s+='\n边：'+this.adj[i][j];
                    }
                }
                s+='\n';
            }

            console.log(s);
        }
    }

    var g=new Graph(5);
    g.addEdge(0,1);
    g.addEdge(0,2);
    g.addEdge(1,3);
    g.addEdge(2,4);

    g.show();


</script>
</body>
</html>